package com.xjgc.share;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 小逗号
 * @date: 2023-11-21
 * @description:
 * @since:
 */
@Data
public class ShareParam {
    public static final Long PAGE_NO = 1L;
    public static final Long PAGE_SIZE = 3000L;

    @NotBlank(message = "orgCode单位标识不能为空!")
    private String orgCode;

    @NotBlank(message = "sourceSystem系统标识不能为空!")
    private String sourceSystem;

    @NotBlank(message = "serialNo流水号不能为空!")
    private String serialNo;


    @ApiParam(name = "pageNo", value = "当前页码")
    private Long pageNo = PAGE_NO;

    @ApiParam(name = "pageSize", value = "页行数")
    private Long pageSize = PAGE_SIZE;


    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(name = "createTimeBegin", value = "开始时间", required = true)
    private Date createTimeBegin;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiParam(name = "createTimeEnd", value = "结束时间")
    private Date createTimeEnd = new Date();

    @ApiParam(name = "isAll", value = "是否所有单位")
    private Boolean isAll = false;
    /**
     * 工厂
     */
    private String factoryCodes;
    /**
     * 销售组织
     */
    private String vkorgs;
    /**
     * 利润中心
     */
    private String prctrs;
    /**
     * 公司代码
     */
    private String bukrs;

    @ApiParam(hidden = true)
    public Map<String, Object> buildQueryParam() {
        this.pageNo = (this.pageNo == null || this.pageNo <= 0) ? PAGE_NO : this.pageNo;
        this.pageSize = (this.pageSize == null || this.pageSize <= 0) ? PAGE_SIZE : this.pageSize;
        Map<String, Object> map = new HashMap<>(12);
        map.put("pageNo", this.pageNo);
        map.put("pageSize", this.pageSize);
        map.put("pageStart", (this.pageNo - 1) * this.pageSize);
        /*--*/
        map.put("createTimeBegin", this.createTimeBegin);
        map.put("createTimeEnd", this.createTimeEnd);


        /*--*/
        if (this.isAll) {
            map.put("isAll", true);
        }else{
            if (StringUtils.isNotEmpty(this.factoryCodes)) {
                String[] split = this.factoryCodes.split(",");
                map.put("factoryCodes", Lists.newArrayList(split));
            }
            if (StringUtils.isNotEmpty(this.vkorgs)) {
                String[] split = this.vkorgs.split(",");
                map.put("vkorgs", Lists.newArrayList(split));
            }
            if (StringUtils.isNotEmpty(this.prctrs)) {
                String[] split = this.prctrs.split(",");
                map.put("prctrs", Lists.newArrayList(split));
            }
            if (StringUtils.isNotEmpty(this.bukrs)) {
                String[] split = this.bukrs.split(",");
                map.put("bukrs", Lists.newArrayList(split));
            }
        }


        return map;
    }


}
